#!/usr/bin/python
#coding=utf-8 

import httplib2
httplib2.debuglevel = 0

h = httplib2.Http('.cache')
h.add_credentials('name','password','domain') # using http basic authentication
r,c = h.request('http://example.org/',headers={'cache-control':'no-cache'}) # c is a byte type

def postData():
    from urllib.parse import urlencode
    data = {'name':'bob','comment':'password'}
    resp, content = h.request("http://bitworking.org/news/223/Meet-Ares", "POST", urlencode(data))
    print (resp)
    print (content.decode('utf-8'))

def cookie():
    import urllib.parse
    url = 'http://www.example.com/login'   
    body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}
    headers = {'Content-type': 'application/x-www-form-urlencoded'}
    response, content = h.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))
    headers = {'Cookie': response['set-cookie']}
    url = 'http://www.example.com/home'   
    response, content = http.request(url, 'GET', headers=headers)

def mocklogin():
    from urllib.parse import urlencode
    url = 'https://www.umeng.com/sso/login'
    r,c = h.request(url,'GET')
    sc = c.decode('utf-8')
    import re
    regx = r'<input type="hidden" id="lt" name="lt" value="(\S+?)" />';
    pm = re.search(regx,sc)
    csrf = pm.group(1)
    body = {'username':'277432742@qq.com','password':'xwangwang','lt':csrf,'service':'http://www.umeng.com/apps'}
    r,c = h.request(url,'POST',body=urlencode(body))
    print (c.decode("UTF-8"))

if __name__ == "__main__":
    mocklogin()
